package com.dingup.provider.controller.sys;

import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.model.sys.SysSubject;
import com.dingup.provider.service.sys.SysSubjectService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/11/28.
 */
@RestController()
@RequestMapping("/sys/subject")
public class SysSubjectController {

    @Autowired
    SysSubjectService sysSubjectService;

    @RequestMapping(value = "/get_all_sys_subject.do", method = RequestMethod.POST)
    public void listAllUser(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取所有的科目信息");
        try {
            List<SysSubject> list = sysSubjectService.getAllSysSubject();
            List<SelectBean> res = list.parallelStream().map(s -> new SelectBean(s.getSubjectName(), s.getId())).collect(Collectors.toList());
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_user_sys_subject.do", method = RequestMethod.POST)
    public void updateUserSysSubject(HttpServletRequest request,
                                     HttpServletResponse response,
                                     String userId,
                                     Integer[] sysSubjectIds) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改指定人员科目信息");
        try {
            List<Integer> ids = new ArrayList<>();
            if (sysSubjectIds != null) {
                ids = Arrays.asList(sysSubjectIds);
            }
            sysSubjectService.update(userId, ids);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
